<?php 
/* ================================================================================
 * Portal	 
 *
 * main.php: This is the main file for Portal.
 *
 * Author: Eric Hendrickson (enhendrickson@liberty.edu)
 * Date: 4/25/08
   ================================================================================ */
	
	global $SSO, $Security, $AppID, $User, $DB;
	
	//Check to see if there is a valid session 
	$SessionInfo = $SSO->isValidSession("index.php?action=login", $SSO->getSessionInformation());	
	
	//Grab the user info
	$UserInfo = $User->GetUserInfo($SessionInfo['username']);

	if(!isset($_GET['eventId'])) {
		exit();
	}
		
	if(isset($_POST['btnSubmit'])) {
		$value_array = 			array (
				'username'			=>  $SessionInfo['username'],
				'event_id'			=> 	$_GET['eventId'],
				'signup_datetime'	=> 	'2008-' . $_POST['selMonth'] . '-' . $_POST['selDay'] . ' ' . $_POST['selHour'] . ':00:00',
				'email_user'             => 1  
		);

		$db_string = $DB->compile_db_insert_string($value_array);
		$DB->query("INSERT INTO signups (".$db_string['FIELD_NAMES'].") VALUES (".$db_string['FIELD_VALUES'].")");	

		header("Location: index.php?action=signUpByEvent&eventId=".$_GET['eventId']); 
	}
	
	$sql = "
		select distinct month(slot_datetime) as slotmonth from signup_slots as ss
		 left join signups as s on ss.slot_datetime = s.signup_datetime and ss.event_id = s.event_id
		where ss.event_id = {$_GET['eventId']}  
		and s.signup_datetime is null
	";

	$DB->query($sql);
	$monthsOptions = "";
	while($row = $DB->fetch_row()) {
		$monthsOptions .= '<option value="' . $row['slotmonth'] . '"';
		if($_GET['month'] == $row['slotmonth']) {
			$monthsOptions .= ' selected';					
		}
		$monthsOptions .= '>'. date( 'F', mktime(0, 0, 0, $row['slotmonth'])) .'</option>'; 
	}
	
	if($_GET['month'] != "") {
		$daysOptions = "";
		if((isset($_GET['month']) && trim($_GET['month']) != "") && (isset($_GET['eventId']) && trim($_GET['eventId']) != "")) {
			$sql = "
				select distinct day(slot_datetime) as slotday from signup_slots as ss
				 left join signups as s on ss.slot_datetime = s.signup_datetime and ss.event_id = s.event_id
				where month(slot_datetime) = {$_GET['month']}
				  and ss.event_id = {$_GET['eventId']}  
				and s.signup_datetime is null
			";

			$DB->query($sql);

			while($row = $DB->fetch_row()) {
				$daysOptions .= '<option value="' . $row['slotday'] . '"';
				if($_GET['day'] == $row['slotday']) {
					$daysOptions .= ' selected';					
				}
				$daysOptions .= '>'. $row['slotday'] .'</option>'; 
			}
			
			if($_GET['day'] != "") {
				$sql = "
					select distinct hour(slot_datetime) as slothour from signup_slots as ss
					 left join signups as s on ss.slot_datetime = s.signup_datetime and ss.event_id = s.event_id
					where month(slot_datetime) = {$_GET['month']} and day(slot_datetime) = {$_GET['day']}
					  and ss.event_id = {$_GET['eventId']}  
					and s.signup_datetime is null
					order by slothour desc
				";
	
				$DB->query($sql);
	
				while($row = $DB->fetch_row()) {
					$hoursOptions .= '<option value="' . $row['slothour'] . '"';
					if($_GET['hour'] == $row['slothour']) {
						$hoursOptions .= ' selected';					
					}
					$hoursOptions .= '>';
					
					if($row['slothour'] > 12) {
						$hoursOptions .= $row['slothour'] - 12 . ":00 PM";
					}
					else {	
						if($row['slothour'] == 0) {
							$hoursOptions .= "12:00 AM";
						}
						elseif($row['slothour'] == 12) {
							$hoursOptions .= "12:00 PM";
						}
						else {
							$hoursOptions .= $row['slothour'] . ":00 AM";
						}
					}
					
					$hoursOptions .= '</option>';
				}
			}
		}
	}
	
?>
<script language="javascript1.2" type="text/javascript">
	function getSignUpDaysByEventMonth(month) {
		location.href = "http://" + location.host + "/SignUp/index.php?action=signUpByEvent&eventId=1000000&month=" + month;
	}

	function getSignUpHoursByEventDay(month,day) {
		location.href = "http://" + location.host + "/SignUp/index.php?action=signUpByEvent&eventId=1000000&month=" + month + "&day=" + day;
	}
	
	function getSignUpHoursByEventHour(month,day,hour) {
		location.href = "http://" + location.host + "/SignUp/index.php?action=signUpByEvent&eventId=1000000&month=" + month + "&day=" + day + "&hour=" + hour;	
	}
	
	function clickSignUp(buttonObj) {
		if(buttonObj.form.selMonth.value == "" || buttonObj.form.selDay.value == "" || buttonObj.form.selHour.value == "") {
			return false;	
		}		
		
		message = 'Sign up for ' + 
		buttonObj.form.selMonth.value + '/' + 
		buttonObj.form.selDay.value + ' ' + 
		buttonObj.form.selHour[buttonObj.form.selHour.selectedIndex].innerHTML + '?';
		
		if(confirm(message)) {
			return true;
		} 
		else {
			return false;
		}	
	}
</script>

<style>
<!--
.Field
{
	border:1px solid #8d6727;	
}	
.Button
{
	border:1px solid #8d6727;
	background:#e4c894;
}

-->
</style>

<?php
	$DB->query("select * from signups where username = '" . $SessionInfo['username'] . "' and event_id = " . $_GET['eventId'] ." order by signup_datetime");
	if($DB->get_num_rows()>0) { $signedUp = true; } else { $signedUp = false; }

	if(!$signedUp) {
		include('signUpByEventForm.php');
	}
	else {
?>
<table cellpadding="0" cellspacing="0" border="0" width="100%">
	<tr>
		<td align="center">
			<table>
				<tr>
					<td>
						<span style="font-size:17px;">Thank you for signing up for this event.</span><br><br>
						You are currently signed up for...
						<table cellspacing="20">
						<tr>
						<td>
						<?php
						while($signUpRow = $DB->fetch_row()) {
							$dateSignUp = strtotime($signUpRow['signup_datetime']);
							print "<li>".date('l, F j, Y g:i A', $dateSignUp)."</li>";
						}
						?></td></tr></table>You are currently logged in as <strong><?php print $UserInfo['fullname']; print "  (".$SessionInfo['username'].")"; ?></strong>&nbsp;&nbsp;<a href="/Portal/index.php?action=logout">Log out?</a><br>
						<br>
						<a href="/Portal/index.php">Return to the Portal</a>
					</td>
				</tr>
				<tr>
					<td><br><hr><br><span style="font-size:17px;">Sign Up for Another Slot?</span></td>
				</tr>
				<tr><td><br><?php include('signUpByEventForm.php'); ?></td></tr>
			</table>
		</td>
	</tr>
</table>
<?php
}
?>